export const state = () => ({
    itemId: ''
})

export const mutations = {
    setItemId(state, itemId) {
        state.itemId = itemId
    }
}

export const actions = {
    getItemListByProductId({}, productId) {
        return this.$api.item.getItemListByProductId(productId).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve(res.data.data)
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    getItemByItemId({}, itemId) {
        return this.$api.item.getItemByItemId(itemId).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve(res.data.data)
            }
            else {
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    updateQuantity({}, items) {
        let promises = []
        for(let item of items) {
            console.log(item)
            promises.push(this.$api.item.updateQuantity(item.itemId, item.quantity).then((res) => {
                if(res.data.status === 'OK') {
                    return Promise.resolve('OK')
                }
                else {
                    return Promise.reject('NOK')
                }
            }, (error) => {
                return Promise.reject(error)
            }).catch((error) => {
                return Promise.reject(error)
            }))
        }
        return Promise.all(promises)
    }
}